package wwy.Service;

import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import wwy.Dao.BookDao;
import wwy.Dto.HomeDto;
import wwy.Entity.Book;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class HomeService extends LogicService<BookDao, Book,Long>{
    public HomeService(@Autowired BookDao dao) {super(dao);}

    @Transactional
    public List<HomeDto> getHomeBookInfo(){
        List<Book> books = dao.findAll();
        List<HomeDto> homeDtoS = new ArrayList<>();
        for (Book book : books){
            HomeDto homeDto = new HomeDto();
            homeDto.setBookName(book.getBookName());
            homeDto.setBookAuthor(book.getBookAuthor());
            homeDto.setBookCover(book.getBookCover());
            homeDto.setBookRatings(book.getBookRatings());
            homeDtoS.add(homeDto);
        }
        return homeDtoS;
    }

    @Transactional
    public Book getBookInfo(String bookName) {
        return dao.findByBookName(bookName);
    }
    @Transactional
    public Book getBookById(Long id) {
        return dao.findById(id).orElse(null);
    }

    @Transactional
    public Book getBook(String bookName) {
        return dao.findByBookName(bookName);
    }

    @Transactional
    public String getBookCover(String bookName) {
        return dao.findByBookName(bookName).getBookCover();
    }

    @Transactional
    public String getBookAuthor(String bookName) {
        return dao.findByBookName(bookName).getBookAuthor();
    }

    @Transactional
    public void setLocalBookAddress(String bookName, String localBookAddress) {
        dao.findByBookName(bookName).setLocalBookAddress(localBookAddress);
    }

    private Book createBook(String bookName, String bookAuthor,
                            String bookCover, Double bookRatings,
                            String bookType, String bookAbstract,
                            String bookAddress,
                            Double totalChar) {
        Book book = new Book();
        book.setBookName(bookName);
        book.setBookAuthor(bookAuthor);
        book.setBookCover(bookCover);
        book.setBookRatings(bookRatings);
        book.setBookType(bookType);
        book.setBookAbstract(bookAbstract);
        book.setBookAddress(bookAddress);
        book.setLocalBookAddress("");
        book.setTotalChar(totalChar);
        return book;
    }

    @Transactional
    @EventListener(ApplicationReadyEvent.class)
    @Order(1)
    public void addDefaultHomeInfo(){
        if(dao.count() > 0) return;
        List<Book> books = Arrays.asList(
                createBook("杀死一只知更鸟", "哈珀·李", "homeBook-recommended1.jpg", 5.0, "Fiction, Historical",
                        "《杀死一只知更鸟》的故事发生在大萧条时期美国南方一个静谧的小镇，几桩离奇的疑案彻底打破了几个孩子平静的生活：事件的真凶，怪人的谜底，传言背后的真相……在父亲的指引下，他们在迷雾中寻找真知，在磨难中历练风度，在不公平中积累正气，经历了暴风骤雨般的成长，也感受了人间的温暖与真情",
                        "https://gitee.com/wuwanyang/e-books/raw/master/%E6%9D%80%E6%AD%BB%E4%B8%80%E5%8F%AA%E7%9F%A5%E6%9B%B4%E9%B8%9F%20(Harper%20Lee).epub",
                        272465.71428571426),
                createBook("霍乱时期的爱情", "加西亚·马尔克斯",
                        "homeBook-recommended2.jpg",
                        4.5, "", "", "", 0.0),
                createBook("穷爸爸富爸爸", "罗伯特·清崎",
                        "homeBook-recommended3.jpg",
                        4.0, "", "", "", 0.0),
                createBook("被讨厌的勇气", "岸见一郎，古贺史健",
                        "homeBook-popular1.jpg",
                        5.0, "", "", "", 0.0),
                createBook("我与地坛", "史铁生",
                        "homeBook-popular2.jpg",
                        5.0, "", "", "", 0.0),
                createBook("平凡的世界", "路遥",
                        "homeBook-popular3.jpg",
                        5.0, "", "", "", 0.0),
                createBook("素食者", "[韩] 韩江 (한강) ",
                        "homeBook-new1.jpg",
                        4.0, "", "", "", 0.0),
                createBook("非暴力沟通", "马歇尔·卢森堡",
                        "homeBook-new2.jpg",
                        4.0, "", "", "", 0.0),
                createBook("乌合之众", "古斯塔夫·勒庞",
                        "homeBook-new3.jpg",
                        4.0, "", "", "", 0.0)

        );

        dao.saveAll(books);

    }
}
